/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.workflow;

import com.ivanbiz.injection.CallCtx;
import com.ivanbiz.properties.SISBeanConstants;
import com.ivanbiz.properties.SISConstants;
import com.ivanbiz.properties.SISScreenConstants;
import com.ivanbiz.tools.IvanBiz;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

/**
 *
 * @author Ivan
 */
public class WorkFlowAction extends IvanBiz {

    ActionMessages actionMessages = new ActionMessages();

    public ActionForward workflowList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        WorkflowService workflowService = (WorkflowService) CallCtx.getContext(request).getBean(SISBeanConstants.WORKFLOW_SERVICE);
        try {
            List listWorkflow = workflowService.loadAll();
            request.setAttribute(SISConstants.LIST_WORKFLOW, listWorkflow);
        } catch (Exception ex) {
            Logger.getLogger(WorkFlowAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.WORKFLOW_LIST);
    }

    public ActionForward workflowAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        WorkflowService workflowService = (WorkflowService) CallCtx.getContext(request).getBean(SISBeanConstants.WORKFLOW_SERVICE);
        WorkFlowForm workFlowForm = (WorkFlowForm) form;
        try {
            Workflow workflow = new Workflow();
            workflow.setNamaStage(workFlowForm.getNamaStage());
            workflow.setDeskripsi(workFlowForm.getDeskripsi());
            workflow.setScore(Float.parseFloat(workFlowForm.getScore()));
            workflowService.saveWorkflow(workflow);
            workFlowForm.reset(mapping, request);
        } catch (Exception ex) {
            Logger.getLogger(WorkFlowAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.WORKFLOW);
    }

    public ActionForward workflowEdit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        WorkflowService workflowService = (WorkflowService) CallCtx.getContext(request).getBean(SISBeanConstants.WORKFLOW_SERVICE);
        WorkFlowForm workFlowForm = (WorkFlowForm) form;
        try {
            Workflow workflow = workflowService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            workFlowForm.setNamaStage(workflow.getNamaStage());
            workFlowForm.setDeskripsi(workflow.getDeskripsi());
            workFlowForm.setScore(String.valueOf(workflow.getScore()));
        } catch (Exception ex) {
            Logger.getLogger(WorkFlowAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.WORKFLOW_EDIT);
    }

    public ActionForward workflowUpdate(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        WorkflowService workflowService = (WorkflowService) CallCtx.getContext(request).getBean(SISBeanConstants.WORKFLOW_SERVICE);
        WorkFlowForm workFlowForm = (WorkFlowForm) form;
        try {
            Workflow workflow = workflowService.loadById(workFlowForm.getId());
            workflow.setNamaStage(workFlowForm.getNamaStage());
            workflow.setDeskripsi(workFlowForm.getDeskripsi());
            workflow.setScore(Float.parseFloat(workFlowForm.getScore()));
            workflowService.updateWorkflow(workflow);
            workFlowForm.reset(mapping, request);
        } catch (Exception ex) {
            Logger.getLogger(WorkFlowAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.WORKFLOW);
    }

    public ActionForward workflowDelete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {
        WorkflowService workflowService = (WorkflowService) CallCtx.getContext(request).getBean(SISBeanConstants.WORKFLOW_SERVICE);
        WorkFlowForm workFlowForm = (WorkFlowForm) form;
        try {
            Workflow workflow = workflowService.loadById(Long.parseLong(request.getParameter(SISConstants.ID)));
            workflowService.deleteWorkflow(workflow);
        } catch (Exception ex) {
            Logger.getLogger(WorkFlowAction.class.getName()).log(Level.SEVERE, null, ex);
            actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(SISConstants.GLOBAL_EXCEPTION));
            saveMessages(request, actionMessages);
            return new ActionForward(mapping.getInput());
        }
        return mapping.findForward(SISScreenConstants.WORKFLOW);
    }
}